package appLoader.displayAdmin
{
	
	import appLoader.I_Loader;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.text.TextField;
	
	import mx.logging.ILogger;
	import mx.logging.Log;
	import mx.logging.LogEvent;
	import mx.logging.LogEventLevel;
	
	public class DisplayLoaderItem extends EventDispatcher
	{
		
		public static const LOAD_COMPLETE:String="Load Complete";
		public static const LOAD_PROGRESS:String="Load Progress Update";
		public static const PLAY_FINISHED:String="End of play";
		
		private var _id:String;//identifier
		private var _container:MovieClip;// Container to place Loader Reference
		private var _content:MovieClip;//If the content is SWF cast content
		private var _contentBitmap:Bitmap; 
		private var _loaderAnimProgress:MovieClip;// CARGO LA VARIABLE PARA TOMAR EL CLIP QUE CONTIENE LA NIMACION DE CARGANDO
		private var _stage:Stage;// Stage Reference
		private var _url:String;// Url Location
		private var _loader:Loader;// LoaderClass
		private var _displayLoaded:Boolean;
		private var _itemSizeKb:uint;//Size in bytes
		
		private var _showPercent:Boolean;// Boolean to enabled the show Percent Property
		private var _addStage:Boolean;// Boolean to canvel the added to stage method
		private var _percent:int=0;// percent of Loaded
		
		private static var logger:ILogger = Log.getLogger("libs.DisplayLoaderItem");
		private var added:Boolean;
		
		public function DisplayLoaderItem(id:String, url:String="")
		{
			logger.log(LogEventLevel.INFO, "Display object added: "+id);
			
			_id=id;
			_url=url;
			
			if (_url!="") {
				_loader = new Loader;
				initLoad();
			}

		}
		
		public function initLoad():void {
			if (_url!="") {
				_loader.contentLoaderInfo.addEventListener(Event.OPEN, handleOpen);
				_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, handleProgress);
				_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleComplete);
				_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, handleError);
				_loader.load(new URLRequest(_url));
			}else{
				logger.log(LogEventLevel.INFO, "Url not setted");
			}
		}
		
		private function handleError(event:IOErrorEvent):void
		{
			logger.log(LogEventLevel.ERROR, "This file : "+ _url +" can't be loaded");
		}
		
		private function handleOpen(event:Event):void{
			
		}
		
		private function handleProgress(event:ProgressEvent):void {
			_percent=Math.round(event.bytesLoaded/event.bytesTotal*100);
			dispatchEvent(new Event(DisplayLoaderItem.LOAD_PROGRESS));
		}
		
		private function handleComplete(event:Event):void {
			if (_loader.contentLoaderInfo.content is MovieClip) {
				_content =new MovieClip;
				_content= _loader.contentLoaderInfo.content as MovieClip;
			} else if (_loader.contentLoaderInfo.content is Bitmap) {
				_contentBitmap = new Bitmap;
				_contentBitmap = _loader.contentLoaderInfo.content as Bitmap;
				_content =new MovieClip;
				_content.addChild(_contentBitmap);
			}
			if (_addStage) {
				addToStage();
			} else if (container!=null) {
				addToStage();
			} else {
				logger.log(LogEventLevel.INFO, "Display Object loaded but not added to stage");
			}
			_displayLoaded=true;
			dispatchEvent(new Event(DisplayLoaderItem.LOAD_COMPLETE));
		}
		
		public function addToStage():void
		{
			_container.addChild(_content);	
			added=true;
		}
		
		public function playContent():void {
			if(_container!=null){
				if(!added){
					addToStage();
				}
			}
			_content.addEventListener(Event.ENTER_FRAME, checkFinish);
			_content.gotoAndPlay(1);
		}
		
		protected function checkFinish(event:Event):void
		{
			if(_content.currentFrame == _content.totalFrames) {
				_content.removeEventListener(Event.ENTER_FRAME, checkFinish);
				_content.gotoAndStop(1);
				dispatchEvent(new Event(DisplayLoaderItem.PLAY_FINISHED));
			}
		}
		
		public function get displayLoaded():Boolean
		{
			return _displayLoaded;
		}

		public function set displayLoaded(value:Boolean):void
		{
			_displayLoaded = value;
		}

		public function get id():String
		{
			return _id;
		}

		public function set id(value:String):void
		{
			_id = value;
		}

		public function get itemSizeKb():uint
		{
			_itemSizeKb=_loader.contentLoaderInfo.bytesTotal;
			return _itemSizeKb;
		}

		public function set itemSizeKb(value:uint):void
		{
			//_itemSizeKb = value;
		}

		public function get addStage():Boolean
		{
			return _addStage;
		}

		public function set addStage(value:Boolean):void
		{
			_addStage = value;
		}

		public function get content():MovieClip
		{
			return _content;
		}

		public function set content(value:MovieClip):void
		{
			_content = value;
		}

		public function get contentBitmap():Bitmap
		{
			return _contentBitmap;
		}

		public function set contentBitmap(value:Bitmap):void
		{
			_contentBitmap = value;
		}

		public function get percent():int
		{
			return _percent;
		}

		public function set percent(value:int):void
		{
			_percent = value;
		}
		
		public function getPercentLoaded():int {
			
			var result:int = 0;
			return result;
		}
		
		public function isLoaded():Boolean{
			
			var result:Boolean;
			return result;
		}

		public function get container():MovieClip
		{
			return _container;
		}

		public function set container(value:MovieClip):void
		{
			_container = value;
		}

	}
}